package com.flashcash.solu.widget.selfdefdialog;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.TextView;

import com.flashcash.solu.R;

/**
 * @作者:My
 * @创建日期: 2017/4/6 10:06
 * @描述:${TODO}
 * @更新者:${Author}$
 * @更新时间:${Date}$
 * @更新描述:${TODO}
 */

public class XLeoProgressDialog extends AlertDialog {
    private final boolean mViewIsDefault;
    private ObjectAnimator mLoadAnimation;
    private TextView mMessage;
    private View mView;
    private static LocalHandle sHandler;
    private OnDialogChange mOnDialogChangeListener;

    public XLeoProgressDialog(Context context) {
        this(context, -1);
    }

    public XLeoProgressDialog(Context context, int theme) {
        this(context, -1, theme);
    }

    public XLeoProgressDialog(Context context, int layoutRes, int theme) {
        super(context, theme == -1 ? R.style.style_bg_transparent_dialog : theme);
        mViewIsDefault = layoutRes == -1;
        mView = LayoutInflater.from(context).inflate(layoutRes == -1 ? R.layout.progress_dialog_loading : layoutRes, null, false);
//        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
//                (int) (context.getResources().getDisplayMetrics().widthPixels * 0.8f),
////                LinearLayout.LayoutParams.WRAP_CONTENT,
//                LinearLayout.LayoutParams.WRAP_CONTENT
//        );
//        params.gravity = Gravity.CENTER;
//        mView.setLayoutParams(params);
        if (mViewIsDefault) {
            initView(mView);
        }
//        setView(mView);
    }

    private LocalHandle getHandler() {
        if (sHandler == null) {
            synchronized (XLeoProgressDialog.class) {
                if (sHandler == null) {
                    sHandler = new LocalHandle();
                }
            }
        }
        return sHandler;
    }

    private void initView(View view) {
        mMessage = (TextView) view.findViewById(R.id.tv_dialog_message);
        View quan = view.findViewById(R.id.iv_dialog_quan);
        mLoadAnimation = ObjectAnimator.ofFloat(quan, "rotation", 0, 360);
        mLoadAnimation.setDuration(1000);
        mLoadAnimation.setRepeatCount(Animation.INFINITE);
        setOnChangeListener(new OnDialogChange() {
            @Override
            public void onShow(View v, Dialog dialog) {
                if (mViewIsDefault && !mLoadAnimation.isStarted()) {
                    if (Looper.myLooper() == null) {
                        getHandler().startAnimation(mLoadAnimation);
                    } else {
                        mLoadAnimation.start();
                    }

                }
            }

            @Override
            public void onHide(View v, Dialog d) {
                if (mViewIsDefault && mLoadAnimation.isStarted()) {
                    if (Looper.myLooper() == null) {
                        getHandler().cancelAnimator(mLoadAnimation);
                    } else {
                        mLoadAnimation.cancel();
                    }
                }
            }

            @Override
            public void onDismiss(View v, Dialog dialog) {
                if (mViewIsDefault && mLoadAnimation.isStarted()) {
                    if (Looper.myLooper() == null) {
                        getHandler().cancelAnimator(mLoadAnimation);
                    } else {
                        mLoadAnimation.cancel();
                    }
                }
            }

            @Override
            public void onCancel(View v, Dialog dialog) {

            }
        });
    }

    public void setOnChangeListener(final OnDialogChange l){
        mOnDialogChangeListener = l;
    }
    @Override
    public void setMessage(CharSequence message) {
        if (mViewIsDefault) {
            mMessage.setText(message);
        }else{
            super.setMessage(message);
        }
    }

    @Override
    public void show() {
        if (mOnDialogChangeListener != null) {
            mOnDialogChangeListener.onShow(mView,XLeoProgressDialog.this);
        }
        super.show();
        setContentView(mView);

    }

    @Override
    public void hide() {
        if (mOnDialogChangeListener != null) {
            mOnDialogChangeListener.onHide(mView,XLeoProgressDialog.this);
        }
        super.hide();
    }

    @Override
    public void dismiss() {
        if (mOnDialogChangeListener != null) {
            mOnDialogChangeListener.onDismiss(mView,XLeoProgressDialog.this);
        }

        super.dismiss();
    }

    static class LocalHandle extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                if (msg.obj != null && msg.obj instanceof Animator) {
                    ((Animator) msg.obj).cancel();
                }
            } else {
                if (msg.obj != null && msg.obj instanceof Animator) {
                    ((Animator) msg.obj).start();
                }
            }
        }

        public void startAnimation(Animator animator) {
            Message msg = obtainMessage();
            msg.obj = animator;
            msg.what = 1;
            sendMessage(msg);
        }

        public void cancelAnimator(Animator animator) {
            Message msg = obtainMessage();
            msg.obj = animator;
            msg.what = 0;
            sendMessage(msg);
        }
    }
}
